package com.app.controller.backstage;

import com.app.mapper.CategoryMapper;
import com.app.model.AdminUserModel;
import com.app.model.CategoryModel;
import com.app.model.SceneryModel;
import com.app.service.AdminUserService;
import com.app.service.SceneryService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 全域⻛光
 */
@Controller
@RestController
@RequestMapping("/admin")
public class AdminSceneryController {
    @Autowired
    private SceneryService sceneryService;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private AdminUserService adminUserService;

    //类型列表
    @GetMapping("/sceneryTypeList")
    public Result<Object> sceneryTypeList(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", 4);
        return Result.success(categoryMapper.selectList(queryWrapper));
    }

    //新增和编辑
    @PostMapping("/insertAndUpdateScenery")
    public Result<Object> insertAndUpdateVideo(@RequestBody SceneryModel model, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        if (model.getId() > 0) {
            model.setUpdateTime(CommonHelp.getNowTimestamp());
            sceneryService.update(model);
        } else {
            model.setCreateTime(CommonHelp.getNowTimestamp());
            sceneryService.insert(model);
        }
        return Result.success("操作成功");
    }

    //删除
    @GetMapping("/deleteScenery")
    public Result<Object> deleteVideo(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        sceneryService.deleteById(id);
        return Result.success("操作成功");
    }

    //列表
    @GetMapping("/getSceneryList")
    public Result<Object> getVideoList(Integer page, Integer limit, String keyword, Integer type, Integer township, HttpServletRequest request) {
        Double longitude = null;
        Double latitude = null;
        if (request.getHeader("longitude") != null) {
            longitude = Double.parseDouble(request.getHeader("longitude"));
        }
        if (request.getHeader("latitude") != null) {
            latitude = Double.parseDouble(request.getHeader("latitude"));
        }
        return Result.success(sceneryService.getList(page, limit, keyword, type, longitude, latitude,township));
    }

    //查看详情
    @GetMapping("/getSceneryInfo")
    public Result<Object> getVideoInfo(Integer id) {
        return Result.success(sceneryService.selectById(id));
    }

    //更新状态
    @GetMapping("/examineScenery")
    public Result<Object> examineScenery(Integer id, Integer status, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        SceneryModel model = new SceneryModel();
        model.setId(id);
        model.setStatus(status);
        sceneryService.update(model);
        return Result.success("更新成功");
    }

}
